//
//  HWHomeHeavyRecommendCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/24.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWHomeHeavyRecommendCell: UITableViewCell {
    
    var collectionView:UICollectionView!
    let collectionProtocol = HWHeavyCellProtocol()
    var model:HWHomeModel?{
        didSet {
            self.collectionProtocol.model = model
            self.collectionView.reloadData()
        }
    }
    var didClickHeavyItem:((_:HWHomeTuiJianModel)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HWHomeHeavyRecommendCell) {
        let reUse:String = "HWHomeHeavyRecommendCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeHeavyRecommendCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeHeavyRecommendCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        
        initFrame()
        
        addAction()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = .white
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HWHeavyGameCell.self, forCellWithReuseIdentifier: "HWHeavyGameCell")
        collectionView.register(HWHeavyLiveCell.self, forCellWithReuseIdentifier: "HWHeavyLiveCell")
        contentView.addSubview(collectionView)
    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(10*widthScale).priority(.low)
            make.bottom.equalTo(-18*widthScale)
            make.left.right.equalToSuperview()
            make.height.equalTo(105*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWHomeHeavyRecommendCell {
    func addAction() -> Void {
        self.collectionProtocol.didClickHeavyItem = {
            mod in
            self.didClickHeavyItem?(mod)
        }
    }
}
